<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">关键字</label>
                <div class="layui-input-inline">
                    <input type="text" name="keyWord" placeholder="商品名称" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="report-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="table" lay-filter="table-filter"></table>
    </div>
</div>

</body>

<!--<script type="text/html" id="table-toolbar">-->
<!--    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchReport">-->
<!--        <i class="layui-icon layui-icon-download-circle"></i>-->
<!--        导出-->
<!--    </button>-->
<!--</script>-->

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script src="../../component/layui_exts/excel.js"></script>
<script>
    layui.use(['table', 'form', 'common', 'easyAdmin'], function () {
        let easyAdmin = layui.easyAdmin;
        let table = layui.table;
        let form = layui.form;

        let cols = [
                [{
                    type: 'checkbox'
                },
                    {
                        title: '统计日期',
                        field: 'reportDate',
                        align: 'center'
                    },
                    {
                        title: '商品ID',
                        field: 'commId',
                        align: 'center'
                    },
                    {
                        title: '商品名称',
                        field: 'commName',
                        align: 'center'
                    },
                    {
                        title: '商品规格',
                        field: 'commSpecsName',
                        align: 'center'
                    },
                    {
                        title: '商品类别',
                        field: 'commTypeName',
                        align: 'center'
                    },
                    {
                        title: '实时库存数量',
                        field: 'invSum',
                        align: 'center'
                    },
                    {
                        title: '入库数量',
                        field: 'invTradeinSum',
                        align: 'center'
                    },
                    {
                        title: '消耗数量',
                        field: 'invTradeoutSum',
                        align: 'center'
                    },
                    {
                        title: '领用人姓名',
                        field: 'receiveUsers',
                        align: 'center'
                    }
                ]
            ]
        ;

        easyAdmin.tableRender({
            elem: '#table', // 对应table的id
            url: '/inv/report',
            page: true, // 分页参数可以自定义
            cols: cols, // 列表示
            skin: 'line', // 表格样式
            //toolbar: '#table-toolbar', // 表格头部工具栏 可定义左上角和右上角工具栏,一般用于左上角配置
            defaultToolbar: [{  // 自由配置头部工具栏右侧的图标按钮
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        // toolbar: '#toolbarDemo'
        // toolbar 点击头部工具栏区域设定了属性为 lay-event="" 的元素时触发(包括左上角和右上角)
        table.on('toolbar(table-filter)', function (obj) {
            switch (obj.event) {
                case 'batchReport':
                    batchReportExcel(obj);
                    break;
                case 'refresh':
                    easyAdmin.tableRefresh('table');
                    break;
            }
        });

        /**
         * 导出
         */
        let batchReportExcel = function (obj) {
            let head = [];
            let headLine = [];
            headLine.push("统计日期");
            headLine.push("商品ID");
            headLine.push("商品名称");
            headLine.push("商品规格");
            headLine.push("商品类别");
            headLine.push("实时库存数量");
            headLine.push("入库数量");
            headLine.push("消耗数量");
            headLine.push("领用人姓名");
            head.push(headLine);
            let body = [];
            let exportData = [];
            // 获取头部
            exportData.push.apply(exportData, head)

            let keyWordVal = $("input[name='keyWord']").val();
            console.log("====================================================" + keyWordVal)
            easyAdmin.http({
                url: '/inv/report/getAll',
                dataType: 'json',
                contentType: 'application/json',
                data: "keyWord=" + keyWordVal,
                type: 'GET',
                success: function (result) {
                    if (result.success) {
                        let allData = result.data;
                        for (let i = 0; i < allData.length; i++) {
                            let aaa = [];
                            aaa.push(allData[i]['reportDate']);
                            aaa.push(allData[i]['commId']);
                            aaa.push(allData[i]['commName']);
                            aaa.push(allData[i]['commSpecsName']);
                            aaa.push(allData[i]['commTypeName']);
                            aaa.push(allData[i]['invSum']);
                            aaa.push(allData[i]['invTradeinSum']);
                            aaa.push(allData[i]['invTradeoutSum']);
                            aaa.push(allData[i]['receiveUsers']);
                            body.push(aaa);
                        }
                    }

                    // 获取body
                    exportData.push.apply(exportData, body)
                    console.log(exportData)
                    // 生成文件
                    LAY_EXCEL.exportExcel(exportData, '流水-' + new Date().toLocaleString() + '.xlsx', 'xlsx')
                }
            });
        }

        // 触发submit提交 按钮点击或者表单被执行提交时触发
        form.on('submit(report-query)', function (data) {
            table.reload('table', {
                // //设定异步数据接口的额外参数
                where: data.field
            })
            return false; // 阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    })
</script>
</html>
